<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <link rel="icon" type="image/x-icon" th:href="${application.iconPath}"/>
    <title>用户--角色配置</title>
    <div th:include="../templates/common::html_header"></div>
    <div th:include="../templates/common::tree"></div>
    <style type="text/css">
        li {
            list-style: none;
        }

        .op_right button {
            margin-top: 10px;
        }
    </style>
</head>

<body>
<div class="layui-form contain ">
    <!--<blockquote class="layui-elem-quote layui-quote-nm"></blockquote>-->
    <div class="layui-form contain ">
        <div style="margin: 30px; font-size: 16px;">

            <div style="float: left; width: 300px; margin-top: -20px; margin-left: 20px;">
                <div id="container" class="zTreeDemoBackground left" style="width: 100%; height: auto;">
                    <ul id="treeDemo" class="ztree" style="width: 100%; height: auto;"></ul>
                </div>
            </div>
            <div style="float: left; margin-left: 30px;" class="op_right">
                <button class="layui-btn layui-btn-sm layui-btn-normal" id="save">
                    保存
                </button>
                <br/>
                <button class="layui-btn layui-btn-sm layui-btn-primary" id="shuaxin">
                    <i class="layui-icon" style="">&#x1002;</i>
                    &nbsp;刷新
                </button>
            </div>
        </div>
    </div>

</div>
</body>


</html>
<div th:include="../templates/common::html_footer"></div>
<input type="hidden" id="account" th:value="${account}"/>
<script>
    var account = $("#account").val(); // 角色编号

    layui.use(['element', 'form', 'layer', 'web'], function () {
        var element = layui.element;
        var form = layui.form;
        var layer = layui.layer;
        var web = layui.web;

        function searchinfo() {

            $("#treeDemo").html("<img  style='margin: 50px;' src='" + basePath + "/istyle/images/load.gif' />");
            var url = basePath + '/ssorole/findRoleTreeByAccount';
            web.ajax(url, 'post', {
                account: account
            }, function (data) {
                var jsoninfo = data.result;
                $.fn.zTree.init($("#treeDemo"), setting, jsoninfo);
            });

        }

        $("#shuaxin").click(function () {
            searchinfo();
        });
        $("#save").click(function () {
            saveTreeInfo();
        });
        searchinfo();
        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                    // 设置数据结构
                    , idKey: "id"
                    , pIdKey: "parentId"
                    , rootPId: null
                }
            }
        };

        function saveTreeInfo() {
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes(true);
            var treeIds = new Array();
            for (var i = 0; i < nodes.length; i++) {
                treeIds[i] = nodes[i].id;
            }
            var url = basePath + '/ssorole/saveRoleTreeData';
            web.load();
            web.ajax(url, 'post', {
                account: account
                , roleCodes: treeIds.toString()
            }, function (data) {
                layer.alert("修改成功", {
                    icon: 1
                }, function (index) {
                    layer.close(index);
                    searchinfo();
                });
            });
        }

    });
</script>
